<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Console\Helper\Table;

class MinuteClassController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
//    public function index()
//{
////        $res = DB::select('select `id`,`name`,`pid`,`path`,`display` from `class`');
//
//    $res = DB::select('select `id`,`name`,`pid`,`path`,`display` from `class` where `pid` = ?', [0]);
//
//
//    return view('admin/classification',['res' => $res]);
//
//
//}


 public function index()
{

//分页
$res = DB::table('class')->where('pid',0)->paginate(2);

     //$result = DB::select('select * from `class`');


return view('admin/classification',['res' => $res]);


}












    //模态框ajax 数据查询
    public function checkajax($id)
    {
        // 获取id
        $id = "$id";

        $result = DB::select('select `id`,`path`,`pid`,`name`,`display` from `class` where `id` = ?', [$id]);

        $pid = $result[0]->pid;
        $path = $result[0]->path;
        $id = $result[0]->id;
        $name = $result[0]->name;
        $display = $result[0]->display;

        return [$pid,$path,$id,$name,$display];
    }

    //添加子分类
    public function subclasses(Request $request)
    {

            $id = $request->id;
            $pid = $request->pid;

            //pid 要等于父级的id
            $fid = $request->id;

            $name = $request->name;
            $p = $request->path;
            $path = $p.$id;
            $display = $request->display;

        $res = DB::insert('insert into `class`(name,pid,path,display) values (?,?,?,?)', [$name, $fid, $path, $display]);

        if($res) {
              $url = url()->previous();
              echo $url;
//            return redirect("adm/class");
              return redirect($url);

        }else{


            echo  '失败';
        }
    }



    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */

    //添加分类
//    public function store(Request $request)
//    {
//        //获取数据
//        $name = $request ->name;
//        $pid = $request ->pid;
//        $path = $request ->path;
//        $display = $request ->display;
//
//        $res = DB::insert('insert into `class`(name,pid,path,display) values (?,?,?,?)', [$name, $pid, $path, $display]);
//
//        if ($res){
//            return $this->index();
//        }
//    }








    //添加分类
    public function store(Request $request)
    {
        //获取数据
        $name = $request ->name;
        $pid = $request ->pid;
        $path = $request ->path;
        $display = $request ->display;

        $res = DB::insert('insert into `class`(name,pid,path,display) values (?,?,?,?)', [$name, $pid, $path, $display]);

        if ($res){

            return redirect('/notice')->with(['message'=>'修改成功','url' =>'adm/class', 'jumpTime'=>3,'status'=>true]);
        }
    }



    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */

    // 子分类查询
    public function show($id)
    {

        $id = $id;
//        $res = DB::select('select `id`,`name`,`path`,`pid`,`display` from `class` where `pid` = ?', [$id]);
        $res = DB::table('class')->where('pid',$id)->simplePaginate(2);
        if ($res) {

            return view('admin/class_operating/childclass',['res' => $res]);

        }else{

            return view('admin/class_operating/childclass');
        }


//        if ($res) {
//
//            return view('admin/class_operating/page',['res' => $res]);
//        }else{
//
//            return view('admin/class_operating/page');
//        }

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request)
    {
        $pid = $request->pid;
        $id = $request->id;
        $name = $request->name;
        $display = $request->display;
        $res = DB::update("update `class` set  `name` = '$name' , `display` = $display where id = ?",[$id]);

        if ($res) {

            return $this->show($pid);

        }else{
            echo '失败';
        }
    }


    //编辑yi及分类
    public  function edit_1(Request $request)
    {
        $id = $request->id;
        $name = $request->name;
        $display = $request->display;
        $res = DB::update("update `class` set  `name` = '$name' , `display` = $display where id = ?",[$id]);

        if($res) {

            return $this->index();
        }
    }

    /**dd
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */

    //删除
    public function destroy($id)
    {
        //执行查询判断是否有子分类
//        $result = DB::table('class')->where('path','like',"0,$id%")->get();

        $result = DB::select('select * from `class` where `pid` = ?', [$id]);

        if ($result) {

            echo '3';

        }else{

            $id = $id;
            $res = DB::delete("delete from `class` where `id` =?",[$id]);
            if ($res){
                echo '1';
            }else{

                echo '2';
            }

        }
//



    }


    //修改状态
    public function edstatic($id,$display,$pid)
    {
        $dis = $display == 1 ?'2':'1';

        $res = DB::update("update `class` set  `display` = $dis where id = ?",[$id]);

        if ($res) {
//            return $this->show($pid);
            return redirect(url()->previous());
        }else{
            echo '失败';
        }
    }

    //查询一级分类数据
    public function chaclass($id)
    {
        $res = DB::select('select `id`,`name`,`path`,`pid`,`display` from `class` where `id` = ?', [$id]);

        return view('admin/class_operating/editclass',['res' => $res]);
    }


    //添加一级分类时 查询是否已存在分类名
    public  function ajaxclass($name)
    {
        //执行条件查询
        $res = DB::select('select `id`,`name`,`path`,`pid`,`display` from `class` where `name` = ?', [$name]);

        if ($res) {
            echo '1';
        }else{
            echo '2';
        }

    }


}
